﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Text;


namespace T.FAS.Runtime.Metadata.Service
{
    using T.FAS.Runtime.Base.Entity;
    //using T.FAS.DataModel.Action.Plugin;
    using T.FAS.Runtime.Metadata.Dao;
    using T.FAS.Runtime.Metadata.Entity;    
    using T.FAS.Runtime.Metadata.SPI;
    using T.ServiceComponent.Error;

    /// <summary>
    /// 元数据运行时服务
    /// </summary>
    public class MeteDataService : IMetaDataService
    {
        TMetaDataDao metaDataDao;               
        MetaDataContentManager contentManager;

        public MeteDataService()
        {
            metaDataDao = new TMetaDataDao();            
            contentManager = new MetaDataContentManager();            
        }

        public IMetaData GetMetaData(string metaDataId)
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MeteDataService.GetMetaData.metaDataId.");

            var metaData = metaDataDao.GetRTPMetaDataBasicInfoByMetaDataID(metaDataId,null);

            if (metaData != null)
            {
                metaData.Content = contentManager.GetMetaDataContent(metaData.ID, metaData.MetaTypeID);
                metaData.MetaDataManageInfos = new List<MetaDataManageInfo>() { new MetaDataManageInfoDao().GetMetaDataManageInfo(metaData.ID) };
            }
            else
            {
                throw new TException("FAS-Meta-1001", $"从DB中获取元数据({metaDataId})失败！");
            }
            if (metaData.Content != null)
            {
                metaData.Content.MetaData = metaData;
                metaData.Content.LastModifyTime = metaData.LastModifyTime;
            }
          
            return metaData;
        }
        public IMetaData GetMetaDataByCode(string metaDataCode, string metaTypeID)
        {
            if (string.IsNullOrWhiteSpace(metaDataCode) || string.IsNullOrWhiteSpace(metaTypeID))
                throw new ArgumentNullException("MeteDataService.GetMetaData.metaDataCode.");

            var metaData = metaDataDao.GetRTPMetaDataBasicInfoByMetaDataID(metaDataCode, metaTypeID);

            if (metaData != null)
            {
                metaData.Content = contentManager.GetMetaDataContent(metaData.ID, metaData.MetaTypeID);
                metaData.MetaDataManageInfos = new List<MetaDataManageInfo>() { new MetaDataManageInfoDao().GetMetaDataManageInfo(metaData.ID) };
            }
            else
            {
                throw new TException("FAS-Meta-1001", $"从DB中获取元数据({metaDataCode})失败！");
            }
            if (metaData.Content != null)
            {
                metaData.Content.MetaData = metaData;
                metaData.Content.LastModifyTime = metaData.LastModifyTime;
            }

            return metaData;
        }

        public TResult CreateInstance(string metaDataId)
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MeteDataService.CreateInstance.metaDataId.");

            var metadata = GetMetaData(metaDataId);

            switch (metadata.MetaTypeID.ToLower())
            {
                case "compositedataobject":
                //    var cdofactory = new CDOSchemaBuildFactory();
                //    var schema = metadata.Content as composite
                //    schema.ResultType = DataType.Record;

                //    return TResultSchemaBuilder.BuildEmptyTResultBySchema(schema);
                case "entityobject":
                //    var eofactory = new EOSchemaBuildFactory();
                //    var eoschema = eofactory.Build(metadata.Content);
                //    eoschema.ResultType = DataType.Record;

                //    return TResultSchemaBuilder.BuildEmptyTResultBySchema(eoschema);
                default:
                    throw new NotSupportedException(metadata.MetaTypeID);
            }
        }

        /// <summary>
        /// 获取指定版本的元数据
        /// </summary>
        /// <param name="metaDataId">元数据ID</param>
        /// <param name="version">版本</param>
        /// <returns>元数据</returns>
        public IMetaData GetMetaDataByVersion(string metaDataId, string version)
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MeteDataService.GetMetaData.metaDataId.");
            if (string.IsNullOrWhiteSpace(version))
                throw new ArgumentNullException("MeteDataService.GetMetaData.version.");

            var metaData = metaDataDao.GetMetaDataByVersion(metaDataId, version);

            if (metaData != null)
                metaData.Content = contentManager.GetMetaDataContent(metaDataId, metaData.MetaTypeID);

            if (metaData != null && metaData.Content != null)
            {
                metaData.Content.MetaData = metaData;
            }

            return metaData;
        }

        public IMetaDataContent GetMetaDataContent(string metaDataId)
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MeteDataService.GetMetaDataContent.metaDataId.");

            var metaData = GetMetaData(metaDataId);

            if (metaData == null) return null;

            return metaData.Content;
        }

        public IMetaDataContent GetMetaDataContentByVersion(string metaDataId, string version)
        {
            if (string.IsNullOrWhiteSpace(metaDataId))
                throw new ArgumentNullException("MeteDataService.GetMetaDataContentByVersion.metaDataId.");
            if (string.IsNullOrWhiteSpace(version))
                throw new ArgumentNullException("MeteDataService.GetMetaDataContentByVersion.version.");

            var metaData = GetMetaDataByVersion(metaDataId, version);

            if (metaData == null) return null;

            return metaData.Content;
        }
    }
}
